import React, { Component } from 'react'
import { Route } from 'react-router-dom'
import { Redirect } from 'react-router-dom/cjs/react-router-dom.min'
import { getToken } from 'utils/storage'

export default class AuthRoute extends Component {
  render() {
    const { component: Component, ...rest } = this.props
    return (
      <Route
        {...rest}
        render={(props) => {
          const token = getToken()
          console.log(props)
          if (token) return <Component {...props} />
          return (
            <Redirect
              to={{
                pathname: '/login',
                state: {
                  fromPath: props.location.pathname,
                },
              }}
            />
          )
        }}
      />
    )
  }
}
